
#include "detect_loop.h"

void detect_loop::yolo_crack_detect_imitate(cv::Mat &img_to_crack , cv::Mat & crack_result)
{
    if(img_to_crack.cols != 4096 || img_to_crack.rows != 3000 )
    {
        cv::resize(img_to_crack, img_to_crack, cv::Size(4096, 3000), 0, 0, INTER_LINEAR);
    }
    std::vector<cv::Vec4i>   lines_results;
    float up_line = 0;
    float down_line = 0;
    std::vector<cv::Mat> mat_container;

    cv::Mat for_cut = img_to_crack.clone();
    if(for_cut.channels() == 1)
    {
        std::vector<cv::Mat> channels_of_clone;
        channels_of_clone.push_back(for_cut.clone());
        channels_of_clone.push_back(for_cut.clone());
        channels_of_clone.push_back(for_cut.clone());
        merge(channels_of_clone,for_cut);     
    }

    cv::Mat image_result_up;
    cv::Mat image_result_med;
    cv::Mat image_result_down;
    cv::Mat image_result;
    try{
        lines_results = line_detecter_ptr -> detect_lines(img_to_crack);
        line_detecter_ptr -> lines_to_line(lines_results, up_line ,down_line);
        if(isnan(up_line) || isnan(down_line))
        {
            up_line = 1000;
            down_line = 2000;
        }
    }catch(...){
        std::cout<<"filed"<<std::endl;
        up_line = 1000;
        down_line = 2000;
    }
    // lines_results = line_detecter_ptr -> detect_lines(img_to_crack);
    // line_detecter_ptr -> lines_to_line(lines_results, up_line ,down_line);
    mat_container = cutter_ptr -> image_cutter(for_cut, int(up_line) , int(down_line));
    for(int i = 0 ; i<mat_container.size() ; i++)
    {
                   //mat_container[0] = 
        vector<YOLOv3::DetectResult> resulter;
        detector_for_crack_ptr -> Detect(mat_container[i],resulter);
        vector<YOLOv3::DetectResult> resulter2;
        for(int i = 0 ;i<resulter.size(); i++)
        {
          if(resulter[i].mName == "crack")
          {
              resulter2.push_back(resulter[i]);
          }
        }
        detector_for_crack_ptr -> DrawResult(mat_container[i],resulter2);
        //  std::string  name1 =  std::to_string(i) + ".jpg";
        //  imwrite(name1,mat_container[i]);
    }
    if(mat_container.size() == 9)
    {
         cutter_ptr-> image_together_Horizontal(mat_container[0],mat_container[1],image_result_up);
         cutter_ptr-> image_together_Horizontal(image_result_up ,mat_container[2],image_result_up);
         cutter_ptr-> image_together_Horizontal(mat_container[3],mat_container[4],image_result_med);
         cutter_ptr-> image_together_Horizontal(image_result_med ,mat_container[5],image_result_med);
         cutter_ptr-> image_together_Horizontal(mat_container[6],mat_container[7],image_result_down);
         cutter_ptr-> image_together_Horizontal(image_result_down ,mat_container[8],image_result_down);
         //imwrite("012.jpg",image_result_up);
         //imwrite("345.jpg",image_result_down);
         //imwrite("678.jpg",image_result_med);
         ////
         cutter_ptr-> image_together_vertical(image_result_up,image_result_med,image_result);
         cutter_ptr-> image_together_vertical(image_result ,image_result_down,image_result);
         //imwrite("all.jpg",image_result);
    }
    crack_result = image_result.clone();
}

void detect_loop::yolo_crack_detect_on_parameters(cv::Mat & img_to_crack , 
cv::Mat & crack_result,int up,int down)
{
    if(img_to_crack.cols != 4096 || img_to_crack.rows != 3000 )
    {
        cv::resize(img_to_crack, img_to_crack, cv::Size(4096, 3000), 0, 0, INTER_LINEAR);
    }
    //std::vector<cv::Vec4i>   lines_results;
    //float up_line;
    //float down_line;
    std::vector<cv::Mat> mat_container;

    cv::Mat for_cut = img_to_crack.clone();
    if(for_cut.channels() == 1)
    {
        std::vector<cv::Mat> channels_of_clone;
        channels_of_clone.push_back(for_cut.clone());
        channels_of_clone.push_back(for_cut.clone());
        channels_of_clone.push_back(for_cut.clone());
        merge(channels_of_clone,for_cut);     
    }

    cv::Mat image_result_up;
    cv::Mat image_result_med;
    cv::Mat image_result_down;
    cv::Mat image_result;

    //lines_results = line_detecter_ptr -> detect_lines(img_to_crack);
    //line_detecter_ptr -> lines_to_line(lines_results, up_line ,down_line);
    mat_container = cutter_ptr -> image_cutter(for_cut, int(up) , int(down));
    for(int i = 0 ; i<mat_container.size() ; i++)
    {
                   //mat_container[0] = 
        vector<YOLOv3::DetectResult> resulter;
        detector_for_crack_ptr -> Detect(mat_container[i],resulter);
        vector<YOLOv3::DetectResult> resulter2;
        for(int i = 0 ;i<resulter.size(); i++)
        {
          if(resulter[i].mName == "crack")
          {
              resulter2.push_back(resulter[i]);
          }
        }
        detector_for_crack_ptr -> DrawResult(mat_container[i],resulter2);
        //  std::string  name1 =  std::to_string(i) + ".jpg";
        //  imwrite(name1,mat_container[i]);
    }
    if(mat_container.size() == 9)
    {
         cutter_ptr-> image_together_Horizontal(mat_container[0],mat_container[1],image_result_up);
         cutter_ptr-> image_together_Horizontal(image_result_up ,mat_container[2],image_result_up);
         cutter_ptr-> image_together_Horizontal(mat_container[3],mat_container[4],image_result_med);
         cutter_ptr-> image_together_Horizontal(image_result_med ,mat_container[5],image_result_med);
         cutter_ptr-> image_together_Horizontal(mat_container[6],mat_container[7],image_result_down);
         cutter_ptr-> image_together_Horizontal(image_result_down ,mat_container[8],image_result_down);
         //imwrite("012.jpg",image_result_up);
         //imwrite("345.jpg",image_result_down);
         //imwrite("678.jpg",image_result_med);
         ////
         cutter_ptr-> image_together_vertical(image_result_up,image_result_med,image_result);
         cutter_ptr-> image_together_vertical(image_result ,image_result_down,image_result);
         //imwrite("all.jpg",image_result);
    }
    crack_result = image_result.clone();
}

void detect_loop::yolo_v3_introduction_weights()
{
    detector_for_crack_ptr = new YOLOv3::Detecting(this->path_loop);
}

detect_loop::detect_loop(std::string path_loop_it)
{
    path_loop = path_loop_it;
    std::cout<<path_loop<<std::endl;
}

detect_loop::~detect_loop()
{
    delete detector_for_crack_ptr;
    delete result_container_ptr;
    delete cutter_ptr;
    delete line_detecter_ptr;
    detector_for_crack_ptr = NULL;
    result_container_ptr = NULL;
    cutter_ptr = NULL;
    line_detecter_ptr = NULL;
}